package com.xt.wikiperfumapi.model.wrapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xt.wikiperfumapi.model.*;
import com.xt.wikiperfumapi.model.vo.IngredientVo;
import com.xt.wikiperfumapi.model.vo.PerfumeCommentInfoVo;
import com.xt.wikiperfumapi.model.vo.PerfumeVo;
import com.xt.wikiperfumapi.service.impl.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class PerfumerWrapper {

    @Resource
    PerfumeCommentInfoServiceImpl perfumeCommentInfoService;

    @Resource
    PerfumeIngredientRefServiceImpl perfumeIngredientRefService;

    @Resource
    PerfumeViewInfoServiceImpl perfumeViewInfoService;

    @Resource
    IngredientInfoServiceImpl ingredientInfoService;

    @Resource
    UserInfoServiceImpl userInfoService;

    @Resource
    UserCollectPerfumeRefServiceImpl userCollectPerfumeRefService;

    @Resource
    IngredientWrapper ingredientWrapper;

    public PerfumeVo perfumeToVo(PerfumeInfo perfumeInfo, String uuid) {
        PerfumeVo perfumeVo = new PerfumeVo();
        try {
            BeanUtils.copyProperties(perfumeVo, perfumeInfo);
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        perfumeVo.setViewCount(perfumeViewInfoService.list(new QueryWrapper<PerfumeViewInfo>().eq("perfume_id", perfumeInfo.getPerfumeId())).size());//浏览数量
        List<PerfumeIngredientRef> perfumeIngredientRefs = perfumeIngredientRefService.list(new QueryWrapper<PerfumeIngredientRef>().eq("perfume_id", perfumeInfo.getPerfumeId()).eq("is_deleted", 0));
        List<IngredientVo> ingredientVos = new ArrayList<>();
        perfumeIngredientRefs.forEach(el -> {
            ingredientVos.add(ingredientWrapper.ingredientToVo(ingredientInfoService.getById(el.getIngredientId())));
        });
        perfumeVo.setIngredientVos(ingredientVos);//原料列表
        long count = userCollectPerfumeRefService.count(new QueryWrapper<UserCollectPerfumeRef>().eq("user_id", uuid).eq("perfume_id", perfumeInfo.getPerfumeId()));
        perfumeVo.setUserCollect(count == 1);//用户是否收藏
        List<PerfumeCommentInfo> perfumeCommentInfos = perfumeCommentInfoService.list(new QueryWrapper<PerfumeCommentInfo>().eq("perfume_id", perfumeInfo.getPerfumeId()).eq("is_deleted", 0));
        List<PerfumeCommentInfoVo> perfumeCommentInfoVos = new ArrayList<>();
        perfumeCommentInfos.forEach(el -> {
            PerfumeCommentInfoVo commentInfoVo = new PerfumeCommentInfoVo();
            try {
                BeanUtils.copyProperties(commentInfoVo, el);
            } catch (Exception e) {
                log.info(e.getMessage());
            }
            commentInfoVo.setUserInfo(userInfoService.getById(el.getUserId()));
            perfumeCommentInfoVos.add(commentInfoVo);
        });//用户评论
        perfumeVo.setPerfumeCommentInfoVos(perfumeCommentInfoVos);
        return perfumeVo;
    }
}
